package com.flot.yazb.base;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.StyleRes;
import android.view.Display;
import android.view.Gravity;
import android.view.WindowManager;

import com.flot.yazb.R;
import com.flot.yazb.CUIAgent;

/**
 * Created by Chexiangjia-MAC on 17/5/2.
 */

public abstract class BDialog extends Dialog {
    private Context context;
    private int mGravity = Gravity.CENTER;
    private int defaultPadwidth = 0;
    private int defaultPadHeight = 0;
    private boolean islockScreen = false;

    public BDialog(Context context) {
        this(context, R.style.DialogNoAnimalPopup);
        setOwnerActivity((Activity) context);
        this.context = context;
    }
    public BDialog(Context context, @StyleRes int themeResId) {
        super(context, themeResId);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        CUIAgent.fixFontScale(context);
        initListener();
    }

    private void initListener() {
        setOnShowListener(new OnShowListener() {
            @Override
            public void onShow(DialogInterface dialogInterface) {
                WindowManager windowManager = ((Activity) context).getWindowManager();
                Display display = windowManager.getDefaultDisplay();
                WindowManager.LayoutParams lp = getWindow().getAttributes();
                lp.width = (int) (display.getWidth()) - setPadWidth(defaultPadwidth); //设置宽度
                getWindow().setAttributes(lp);
                getWindow().setGravity(setGravity(mGravity));
                setCancelable(isCancelable(true));
            }
        });
    }

    public abstract boolean isCancelable(boolean b);

    public abstract int setGravity(int gravity);

    public abstract int setPadWidth(float width);

    public abstract int setPadHeight(float height);

    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }


}
